//
//  LPFEmoticonPictureViewCell.swift
//  WeiBo
//
//  Created by mac on 16/4/25.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

// 删除按钮点击通知
let LPFEmoticonDeleteButtonNotification = "LPFEmoticonDeleteButtonNotification"

// 表情键盘的表情按钮通知
let LPFEmoticonButtonNSNotification = "LPFEmoticonButtonNSNotification"

class LPFEmoticonPictureViewCell: UICollectionViewCell {
    
    // MARK: - 保存表情按钮数组
    var buttons:[LPFEmoticonButton] = [LPFEmoticonButton]()
    
    // MARK: - 表情一维数组
    var emoticons: [LPFEmoticonModel]?{
        didSet{
            // 遍历我们的buttons(隐藏所有button)
            for button in buttons{
                button.hidden = true
            }
            for (i, emoticon) in emoticons!.enumerate(){
                let button = buttons[i]
                // 显示button
                button.hidden = false
                
                //给我们自定义button属性赋值
                button.emoticon = emoticon
                
                // 代表emoji表情
                if emoticon.isEmoji{
                    // 设置title
                    button.setTitle(((emoticon.code ?? "") as NSString).emoji(), forState: UIControlState.Normal)
                    // 解决cell重用问题
                    button.setImage(nil, forState: UIControlState.Normal)
                }else{
                    // 代表图片表情
                    let s1 = emoticon.path ?? ""
                    let s2 = emoticon.png ?? ""
                    let path = "\(s1)\(s2)"
                    let image = UIImage(named: path, inBundle: LPFEmoticonTools.sharedTools.emoticonBundle, compatibleWithTraitCollection: nil)
                    button.setImage(image, forState: UIControlState.Normal)
                    // 解决cell重用问题
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置视图
    private func setupUI(){
        self.backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        addChildButtons()
        
        contentView.addSubview(deleteButton)
    }
    
    // MARK: - 约束
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // button 的宽 高
        let w = (SCREENW - 10) / CGFloat(LPFEmoticonMaxCol)
        let h = (216 - 37 - 20) / CGFloat(LPFEmoticonMaxRow)
        
        // 设置frame
        // 遍历数组
        for (i, button) in buttons.enumerate(){
            let x = i % LPFEmoticonMaxCol
            let y = i / LPFEmoticonMaxCol
            button.frame = CGRect(x: CGFloat(x) * w + 5, y: CGFloat(y) * h, width: w, height: h)
//            button.backgroundColor = RandomColor()
        }
        // 设置删除按钮的frame
        deleteButton.frame = CGRect(x: SCREENW - 5 - w, y: h * 2, width: w, height: h)
    }
    
    // MARK: - 添加表情按钮
    func addChildButtons(){
        // 创建button
        for _ in 0..<LPFEmoticonMaxCount{
            let button = LPFEmoticonButton()
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            contentView.addSubview(button)
            buttons.append(button)
        }
    }
    
    // MARK: - 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "clickDeleteButton", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
    
    // MARK: - 删除按钮点击事件
    func clickDeleteButton(){
        //发通知 删除文本信息
        NSNotificationCenter.defaultCenter().postNotificationName(LPFEmoticonDeleteButtonNotification, object: nil)
    }
    
    // MARK: - 添加表情按钮点击事件
    func buttonClick(button: LPFEmoticonButton){
        NSNotificationCenter.defaultCenter().postNotificationName(LPFEmoticonButtonNSNotification, object: button.emoticon)
    }
}
